package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.ConferenceTopicDTO;
import com.eastfair.imaster.dto.ConferenceTopicPageQuery;
import com.eastfair.imaster.dto.ConferenceTopicSaveDTO;
import com.eastfair.imaster.dto.ConferenceTopicUpdateDTO;
import com.eastfair.imaster.entity.ConferenceTopic;
import com.eastfair.imaster.service.ConferenceTopicService;
import com.eastfair.imaster.vo.ConferenceTopicVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 论坛主题
 * </p>
 *
 * @author zjn
 * @date 2021-08-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/conferenceTopic")
@Api(value = "ConferenceTopic", tags = "论坛主题")
public class ConferenceTopicController extends SuperController<ConferenceTopicService, Long, ConferenceTopic, ConferenceTopicPageQuery, ConferenceTopicSaveDTO, ConferenceTopicUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private ConferenceTopicService conferenceTopicService;

    @Override
    public void handlerResult(IPage<ConferenceTopic> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ConferenceTopic> conferenceTopicList = list.stream().map((map) -> {
            ConferenceTopic conferenceTopic = ConferenceTopic.builder().build();
            //TODO 请在这里完成转换
            return conferenceTopic;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(conferenceTopicList));
    }

    /**
     * 新增论坛主题
     *
     * @param conferenceTopicDTO 论坛
     * @return 主键
     */
    @ApiOperation(value = "新增论坛", notes = "新增论坛")
    @PostMapping("/saveConferenceTopic")
    public R<Boolean> saveConferenceTopic(@RequestBody @Validated({ConferenceTopicDTO.VerifyInsert.class}) ConferenceTopicDTO conferenceTopicDTO) {
        return conferenceTopicService.saveConferenceTopic(conferenceTopicDTO);
    }

    /**
     * 删除论坛主题
     *
     * @param id 论坛主题id
     * @return 是否成功
     */
    @ApiOperation(value = "删除论坛主题", notes = "删除论坛主题")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteById(@PathVariable("id") Long id) {
        return conferenceTopicService.deleteById(id);
    }

    /**
     * 修改论坛主题
     *
     * @param conferenceTopicDTO 论坛主题
     * @return 是否修改成功
     */
    @ApiOperation(value = "修改论坛主题", notes = "修改论坛主题")
    @PutMapping("/updateById")
    public R<Boolean> updateById(@RequestBody @Validated({ConferenceTopicDTO.VerifyUpdate.class}) ConferenceTopicDTO conferenceTopicDTO) {
        return conferenceTopicService.updateById(conferenceTopicDTO);
    }

    /**
     * 通过id查询论坛主题
     *
     * @param id 论坛主题id
     * @return 论坛主题实体
     */
    @ApiOperation(value = "通过id查询论坛主题", notes = "通过id查询论坛主题")
    @GetMapping("/queryById/{id}")
    public R<ConferenceTopicVO> queryById(@PathVariable("id") Long id) {
        return conferenceTopicService.queryById(id);
    }

    /**
     * 获取分页论坛主题列表
     *
     * @param params 分页条件
     * @return 分页论坛主题列表
     */
    @ApiOperation(value = "获取分页论坛主题列表", notes = "获取分页论坛主题列表")
    @PostMapping("/queryPageList")
    public R<Page<ConferenceTopicVO>> queryPageList(@RequestBody PageParams<ConferenceTopicPageQuery> params) {
        return R.success(conferenceTopicService.queryPageList(params));
    }

    /**
     * 论坛主题列表
     *
     * @param conferenceTopicDTO 筛选条件
     * @return 论坛主题列表
     */
    @ApiOperation(value = "获取分页论坛主题列表", notes = "获取分页论坛主题列表")
    @PostMapping("/queryList")
    public R<List<ConferenceTopicVO>> queryList(@RequestBody ConferenceTopicDTO conferenceTopicDTO) {
        return conferenceTopicService.queryList(conferenceTopicDTO);
    }

    /**
     * 通过id修改启用/禁用状态
     *
     * @param conferenceTopicDTO 论坛主题
     * @return 是否成功
     */
    @ApiOperation(value = "通过id修改启用/禁用状态", notes = "通过id修改启用/禁用状态")
    @PostMapping("/updateEnabledById")
    public R<Boolean> updateEnabledById(@RequestBody ConferenceTopicDTO conferenceTopicDTO) {
        return conferenceTopicService.updateEnabledById(conferenceTopicDTO);
    }

}
